import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/HardwareVersion.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/gen/assets.gen.dart';
import 'package:sgy_flutter/model/product_type.dart';
import 'package:sgy_flutter/page/adjust_zero/view.dart';
import 'package:sgy_flutter/page/detail/view/axpower_center_view.dart';
import 'package:sgy_flutter/page/detail/view/button_view.dart';
import 'package:sgy_flutter/page/detail/view/axo_center_view.dart';
import 'package:sgy_flutter/page/detail/view/button_view_axpower_axo.dart';
import 'package:sgy_flutter/page/detail/view/common_info_view.dart';
import 'package:sgy_flutter/page/detail/view/dls_double_center_view.dart';
import 'package:sgy_flutter/page/detail/view/dls_double_center_view_old.dart';
import 'package:sgy_flutter/page/detail/view/dls_double_top_view.dart';
import 'package:sgy_flutter/page/detail/view/dls_single_center_view.dart';
import 'package:sgy_flutter/page/detail/view/dls_single_top_view.dart';
import 'package:sgy_flutter/page/question/view.dart';
import 'package:sgy_flutter/widget/app_bar_x.dart';
import 'package:sgy_flutter/widget/app_dialog.dart';

import 'logic.dart';
import 'view/axo_top_view.dart';
import 'view/axocc_center_view.dart';
import 'view/dls_center_view_old_16_18.dart';
import 'view/dls_single_center_view_old.dart';

class DetailPage extends StatefulWidget {
  const DetailPage({Key? key}) : super(key: key);

  @override
  State<DetailPage> createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  final logic = Get.put(DetailLogic());

  @override
  Widget build(BuildContext context) {
    printInfo(info: "productType:${productType}");
    return Scaffold(
      appBar: AppBarX(
        title: Text("设备详情".tr),
        centerTitle: true,
        elevation: 0,
        actions: [
          IconButton(
              onPressed: () {
                Get.to(() => QuestionPage());
              },
              icon: Assets.image.iconQuestion.image(width: 20, height: 20)),
        ],
      ),
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          FractionallySizedBox(
            widthFactor: 1,
            heightFactor: 0.65,
            child: Container(
              padding: EdgeInsets.only(bottom: 100),
              color: Get.theme.primaryColor,
              alignment: Alignment.bottomCenter,
              child: Assets.image.welcomeLogo.image(width: 200),
            ),
          ),
          Obx(() =>
              CustomScrollView(
                reverse: logic.isDoubleSlide.value ? false : false,
                physics: BouncingScrollPhysics(),
                slivers: [
                  SliverToBoxAdapter(
                    child: switch (productType) {
                      ProductType.dls =>
                      logic.isDoubleSlide.value
                          ? DlsDoubleTopView()
                          : DlsSingleTopView(),
                      ProductType.axo => AxoTopView(),
                      _ => AxoTopView(),
                    },
                  ),
                  SliverToBoxAdapter(
                    child: switch (productType) {
                      ProductType.dls =>
                          _buildDlsCenterView(logic.isDoubleSlide.value),
                      ProductType.axo => AxoCenterView(),
                      ProductType.axpower => AxpowerCenterView(),
                      ProductType.axocc => AxoccCenterView(),
                    _ => AxoCenterView(),
                    },
                  ),
                  SliverToBoxAdapter(
                    child: CommonInfoView(),
                  ),
                  SliverFillRemaining(
                    hasScrollBody: false,
                    child: Container(
                      color: Colors.white,
                      child: Align(
                        alignment: Alignment.topCenter,
                        child: switch (productType) {
                          ProductType.axocc => ButtonViewAxpowerAxo(),
                          ProductType.axpower => ButtonViewAxpowerAxo(),
                          _ => ButtonView(),
                        },
                      ),
                    ),
                  ),
                ],
              )),
        ],
      ),
    );
  }

  ProductType? get productType =>
      logic.bleManager.deviceModel?.productType ??
          logic.rightBleManager?.deviceModel?.productType;

  Widget _buildDlsCenterView(bool isDoubleSlide) {
    printInfo(
        info:
        '_buildDlsCenterView isDoubleSlide:${isDoubleSlide},:${HardwareVersion
            .isOldDevice(logic.bleManager.deviceModel)},${logic.bleManager
            .deviceModel?.hardware}');
    if (HardwareVersion.isOldDevice(logic.bleManager.deviceModel) ||
        HardwareVersion.isOldDevice(logic.rightBleManager.deviceModel)) {
      if (isDoubleSlide) {
        return OldDlsDoubleCenterView();
      } else {
        if (logic.bleManager.cyclingPowerService16.isSupportNotify ||
            logic.bleManager.cyclingPowerService.isSupportNotify) {
          return DlsCenterViewOld1618();
        } else {
          return OldDlsSingleCenterView();
        }
      }
    } else {
      if (isDoubleSlide) {
        printInfo(
            info:
            '_buildDlsCenterView isDoubleSlide');
        return DlsDoubleCenterView();
      } else {
        printInfo(
            info:
            '_buildDlsCenterView isDoubleSlide');
        return DlsSingleCenterView();
      }
    }
  }

  @override
  void dispose() {
    Get.delete<DetailLogic>();
    super.dispose();
  }
}
